#include "SQLBooleanExpr.h"

#include "../SQLDataType.h"
#include "../SQLDataTypeImpl.h"
#include "../../../sql/visitor/SQLASTVisitor.h"

SQLDataType_ptr SQLBooleanExpr::DATA_TYPE = SQLDataType_ptr(new SQLDataTypeImpl(SQLDataType::Constants::BOOLEAN));

SQLBooleanExpr::SQLBooleanExpr()
{
}

SQLBooleanExpr::SQLBooleanExpr(bool value)
{
  this->value = value;
}

bool SQLBooleanExpr::getBooleanValue()
{
  return value;
}

bool SQLBooleanExpr::getValue()
{
  return value;
}

void SQLBooleanExpr::setValue(bool value)
{
  this->value = value;
}

void SQLBooleanExpr::accept0(SQLASTVisitor_ptr visitor)
{
  visitor->visit(SharedObject(SQLBooleanExpr));
  visitor->endVisit(SharedObject(SQLBooleanExpr));
}

void SQLBooleanExpr::output(std::stringstream * buf)
{
  // try {
  (*buf)<<value ? "BOOL::TRUE" : "BOOL::FALSE";
  // } catch (IOException ex) {
  //     throw new FastsqlException("output error", ex);
  // }
}

int SQLBooleanExpr::hashCode()
{
  int prime = 31;
  int result = 1;
  result = prime * result + (value ? 1231 : 1237);
  return result;
}

bool SQLBooleanExpr::equals(Object_ptr obj)
{
  if (this == obj.get())
  {
    return true;
  }
  if (obj == NULL)
  {
    return false;
  }
  if (getClass() != obj->getClass())
  {
    return false;
  }
  SQLBooleanExpr_ptr other = std::dynamic_pointer_cast<SQLBooleanExpr>(obj);
  if (value != other->value)
  {
    return false;
  }
  return true;
}

SQLDataType_ptr SQLBooleanExpr::computeDataType()
{
  return DATA_TYPE;
}

SQLObject_ptr SQLBooleanExpr::clone()
{
  // return new SQLBooleanExpr(value);
  SQLObject_ptr clone_tmp = SQLBooleanExpr_ptr(new SQLBooleanExpr(value));
  SQLBooleanExpr_ptr clone_res = std::dynamic_pointer_cast<SQLBooleanExpr>(clone_tmp);
  return clone_res;
}

SQLObject_list_ptr SQLBooleanExpr::getChildren()
{
  SQLObject_list_ptr tmp = std::make_shared<std::list<SQLObject_ptr>>();
  return tmp;
};
